package PackageCalculator;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.BreakIterator;

public class Test {
    public Test(){

        //创建窗口
        JFrame jFrame = new JFrame("简单计算器");
        jFrame.setSize(400,300);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setLayout(null);


        //创建标签
        JLabel jLabel1 = new JLabel("操作数1：");
        JLabel jLabel2 = new JLabel("操作数2：");
        JLabel jLabel3 = new JLabel("计算结果：");

        jLabel1.setBounds(20,0,200,100);
        jLabel2.setBounds(20,50,200,100);
        jLabel3.setBounds(20,100,200,100);


        //创建文本框
        JTextField jTextField1 = new JTextField();
        JTextField jTextField2 = new JTextField();
        JTextField jTextField3 = new JTextField();

        jTextField1.setBounds(100,38,150, 30);
        jTextField2.setBounds(100,85,150, 30);
        jTextField3.setBounds(100,135,150, 30);


        //创建按钮
        JButton jButton1 = new JButton("+");
        JButton jButton2 = new JButton("-");
        JButton jButton3 = new JButton("*");
        JButton jButton4 = new JButton("/");


        jButton1.setBounds(300,40 - 3,100,33);
        jButton2.setBounds(300,73 - 3,100,33);
        jButton3.setBounds(300,106 - 3,100,33);
        jButton4.setBounds(300,140 - 3,100,33);


        //给按钮添加监听器
        jButton1.addActionListener( e -> {
                try {
                    jTextField3.setText(String.valueOf(Double.parseDouble(jTextField1.getText()) + Double.parseDouble(jTextField2.getText())));
                } catch (NumberFormatException ex) {

                    jTextField1.setText("");
                    jTextField2.setText("");
                    jTextField3.setText("傻鸟，只能输数字！");
                    ex.printStackTrace();
                }
        });

        jButton2.addActionListener( e -> {
                try {
                    jTextField3.setText(String.valueOf(Double.parseDouble(jTextField1.getText()) - Double.parseDouble(jTextField2.getText())));
                } catch (NumberFormatException ex) {

                    jTextField1.setText("");
                    jTextField2.setText("");
                    jTextField3.setText("傻鸟，只能输数字！");
                    ex.printStackTrace();
                }
        });

        jButton3.addActionListener( e -> {
                try {
                    jTextField3.setText(String.valueOf(Double.parseDouble(jTextField1.getText()) * Double.parseDouble(jTextField2.getText())));
                } catch (NumberFormatException ex) {

                    jTextField1.setText("");
                    jTextField2.setText("");
                    jTextField3.setText("傻鸟，只能输数字！");
                    ex.printStackTrace();
                }
        });

        jButton4.addActionListener( e -> {
                try {
                    jTextField3.setText(String.valueOf(Double.parseDouble(jTextField1.getText()) / Double.parseDouble(jTextField2.getText())));
                } catch (NumberFormatException ex) {

                    jTextField1.setText("");
                    jTextField2.setText("");
                    jTextField3.setText("傻鸟，只能输数字！");
                    ex.printStackTrace();
                }
        });


        //将各种组件添加到窗口上
        jFrame.getContentPane().add(jLabel1);
        jFrame.getContentPane().add(jLabel2);
        jFrame.getContentPane().add(jLabel3);
        jFrame.getContentPane().add(jButton1);
        jFrame.getContentPane().add(jButton2);
        jFrame.getContentPane().add(jButton3);
        jFrame.getContentPane().add(jButton4);
        jFrame.getContentPane().add(jTextField1);
        jFrame.getContentPane().add(jTextField2);
        jFrame.getContentPane().add(jTextField3);


        //设置窗口可见
        jFrame.setVisible(true);


    }

    public static void main(String[] args) {
        new Test();
    }
}
